package omq.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import omq.api.logger.OmqLogger;
import omq.exception.OMQPropertiesReaderException;

//TODO do we need this class ?!?!?!?!?!?

public class OmqPropertiesReader {
        Properties properties;
        private String propertiesFileName=null;
        
        public OmqPropertiesReader(String filePath) throws FileNotFoundException, IOException{
            properties = new Properties();
            propertiesFileName=filePath;
//            try {
                 properties.load(new FileInputStream(filePath));                
//            } catch (FileNotFoundException e) {
//                 OmqLogger.getInstance().fatal(OmqConstants.PROPERTIES_FILE_NOT_LOADED_ERROR);
//            } catch (IOException e) {
//            	OmqLogger.getInstance().fatal(OmqConstants.PROPERTIES_FILE_NOT_READ_ERROR);
//            }
        }      
 
        /**
         * method : getProperty.
         * This method gets the value of a property by the property name sent to it.
         * @param propName - the property name requested by the user.
         * @return - the property value from the file.
         * @throws PropertiesReaderException - occurs when the property name does not
         */
        public String getProperty(String propName) throws OMQPropertiesReaderException{
            String propVal =  properties.getProperty(propName); 
            if(propVal == null){ 
                    throw new OMQPropertiesReaderException("property named " + propName + " was not found in properties file : "+propertiesFileName);
            }
            return propVal;
        }

		public String getPropertiesFileName() {
			return propertiesFileName;
		}

		public void setPropertiesFileName(String propertiesFileName) {
			this.propertiesFileName = propertiesFileName;
		}

		public static OmqPropertiesReader createOMQPropertiesReader(String propertiesFilename) throws OMQPropertiesReaderException
		{
			OmqPropertiesReader propertiesReader;
			try
			{
				if (!propertiesFilename.substring(0,2).equalsIgnoreCase("\\") )
				{
					propertiesFilename = "\\"+propertiesFilename;
				}
				propertiesReader = new OmqPropertiesReader(System.getProperty("user.dir") + propertiesFilename);
	        } 
			catch (FileNotFoundException e) 
			{
				OmqLogger.getInstance().fatal("The properties file "+System.getProperty("user.dir") + propertiesFilename+" could not be loaded",e);
				throw new OMQPropertiesReaderException("The properties file "+System.getProperty("user.dir") + propertiesFilename+" could not be loaded");
	        } 
			catch (IOException e) 
			{
				OmqLogger.getInstance().fatal("The properties file "+System.getProperty("user.dir") + propertiesFilename+" could not be read",e);
				throw new OMQPropertiesReaderException("The properties file "+System.getProperty("user.dir") + propertiesFilename+" could not be read");
	        }
			return propertiesReader;
		}
}